package com.yao.controller;

import com.yao.common.Result;
import com.yao.po.Categorize;
import com.yao.po.Categorize1;
import com.yao.po.Categorize2;
import com.yao.service.ICategorize1Service;
import com.yao.service.ICategorize2Service;
import com.yao.utils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/index")
public class CategorizeController {

    private static final Logger log = LoggerFactory.getLogger(CategorizeController.class);
    private final ICategorize1Service categorize1Service;
    private final ICategorize2Service categorize2Service;

    //    Categorize
    public CategorizeController(ICategorize1Service categorize1Service, ICategorize2Service categorize2Service) {
        this.categorize1Service = categorize1Service;
        this.categorize2Service = categorize2Service;
    }

    /**
     * 查询分类商品
     *
     * @return 数组
     */
    @GetMapping("/findCategoryTree")
    public Result<Object> findCategoryTree() {
        log.info("查询分类商品...");
        List<Categorize1> list1 = categorize1Service.list();

        List<Categorize> stream1 = BeanUtils.copyList(list1, Categorize.class);

        //TODO 这里应该是根据分类id查询分类商品
        List<Categorize2> list2 = categorize2Service.list();

        stream1.forEach(categorize -> {
            List<Categorize2> list = list2.parallelStream()
            .filter(categorize2 ->
            categorize2.getCategorize1() == categorize.getId()).toList();
            categorize.setChildren(list);//设置子分类
        });
        return Result.success(stream1);
    }
}
